package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DataBase {
	public Connection getDBConnection()throws Exception{ 
		  String driverName="com.mysql.jdbc.Driver";
		  String userName="root";
		  String userPwd="password";
		  String dbName="library";
		  String url1="jdbc:mysql://49.232.9.251:3306/"+dbName;//使用服务器上的数据库
		  String url2="?user="+userName+"&password="+userPwd;
		  String url3="&userUnicode=true&characterEncoding=UTF-8";
		  String url=url1+url2+url3;
		  Class.forName(driverName);
		  Connection conn=DriverManager.getConnection(url);
		  return conn;
	  }
	  public void free(Connection conn,PreparedStatement st,ResultSet rs) throws SQLException {
		  try {
			  if (rs != null)
				  rs.close();
		  } catch (SQLException e) {
			  e.printStackTrace();
		  } finally {
			  try {
				  if (st != null)
					  st.close();
			  } catch (SQLException e) {
				  e.printStackTrace();
			  } finally {
				  if (conn != null)
					  try {
						  conn.close();
					  } catch (SQLException e) {
						  e.printStackTrace();
					  }
			  }
		  }
	  }
}
